package org.wenbo.override.tools.page;

public class Sql2000 {
	public static Sql2000 me() {
		return new Sql2000();
	}

	public String getSql(int pageSize, int thisPage, String viewName,
			String fieldName, String desc) {
		/*
		 * SELECT TOP @pageSize FROM @tableName WHERE
		 * 
		 * @fieldName NOT IN (SELECT TOP
		 * 
		 * @pageSize(@page-1) @fieldName FROM @tableName ORDER BY @fieldName ASC
		 * ) ORDER BY @fieldName ASC
		 */

		if (fieldName == null) {
			fieldName = "id";
		}
		String querySQL;

		if (desc.equals("desc")) {
			querySQL = "SELECT TOP %d * FROM %s WHERE %s NOT IN (SELECT TOP %d %s FROM %s ORDER BY %s DESC) ORDER BY %s DESC";
		} else {
			querySQL = "SELECT TOP %d * FROM %s WHERE %s NOT IN (SELECT TOP %d %s FROM %s ORDER BY %s ASC) ORDER BY %s ASC";
		}
		int offSet = pageSize * (thisPage - 1);
		return String.format(querySQL, pageSize, viewName, fieldName, offSet,
				fieldName, viewName, fieldName, fieldName);
	}
}
